package com.smg.community.util;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.smg.community.model.SmgKeyword;
import com.smg.community.setting.Cache;

public class StringUtil {
	
	/**
	 * 判断是否为空
	 * @param str 参数
	 */
	public static boolean isEmpty (String str) {
		if (str == null || str.trim().length() == 0) {
			return true;
		} else {
			return false;
		}
	}
	/** 
     * MD5 加密 
     */  
    public static  String getMD5Str(String str) {  
        MessageDigest messageDigest = null;  
  
        try {  
            messageDigest = MessageDigest.getInstance("MD5");  
  
            messageDigest.reset();  
  
            messageDigest.update(str.getBytes("UTF-8"));  
        } catch (NoSuchAlgorithmException e) {  
            System.out.println("NoSuchAlgorithmException caught!");  
            System.exit(-1);  
        } catch (UnsupportedEncodingException e) {  
            e.printStackTrace();  
        }  
  
        byte[] byteArray = messageDigest.digest();  
  
        StringBuffer md5StrBuff = new StringBuffer();  
  
        for (int i = 0; i < byteArray.length; i++) {              
            if (Integer.toHexString(0xFF & byteArray[i]).length() == 1)  
                md5StrBuff.append("0").append(Integer.toHexString(0xFF & byteArray[i]));  
            else  
                md5StrBuff.append(Integer.toHexString(0xFF & byteArray[i]));  
        }  
  
        return md5StrBuff.toString();  
    }  
    
    public static  Map<String,String> filterKeyword(String cont,	List<SmgKeyword> keywordList){
    	Map<String,String>  resultMap = new HashMap<String,String>();
    	String containKeywords = "";
    	for( SmgKeyword  keyword:keywordList){
    		String keywordString = keyword.getKeyword();
    		if ( keywordString.trim().length() == 0 ){
    			continue;
    		}
    		if ( cont.indexOf(keywordString) != -1){
    			cont  = cont.replaceAll(keywordString, "*");
    			if ( containKeywords.length() == 0){
    				containKeywords = keywordString;
    			}else{
    				containKeywords = containKeywords +","+keywordString;
    			}
    		}else{
    			
    		}
    	}
    	resultMap.put("containKeywords", containKeywords);
    	resultMap.put("cont", cont);
    	return resultMap ;
    	
    }
}
